package com.sign2024.sign2024project.reporttest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.sign2024.sign2024project.reporttest.ReportTest;
import com.sign2024.sign2024project.reporttest.mapper.ReportTestMapper;
import com.sign2024.sign2024project.reporttest.service.ReportTestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2024/7/19 9:22
 */
@Service
public class ReportTestServiceImpl implements ReportTestService {

    @Resource
    private ReportTestMapper reportTestMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ReportTest queryById(Integer id) {
        return reportTestMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param reportTest 筛选条件
     * @param current    当前页码
     * @param size       每页大小
     * @return
     */
    @Override
    public Page<ReportTest> paginQuery(ReportTest reportTest, long current, long size) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<ReportTest> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(reportTest.getSignReport())) {
            queryWrapper.eq(ReportTest::getSignReport, reportTest.getSignReport());
        }
        if (StrUtil.isNotBlank(reportTest.getTestInfo())) {
            queryWrapper.eq(ReportTest::getTestInfo, reportTest.getTestInfo());
        }
        if (StrUtil.isNotBlank(reportTest.getMerge())) {
            queryWrapper.eq(ReportTest::getMerge, reportTest.getMerge());
        }
        if (StrUtil.isNotBlank(reportTest.getBigMerge())) {
            queryWrapper.eq(ReportTest::getBigMerge, reportTest.getBigMerge());
        }
        //2. 执行分页查询
        Page<ReportTest> pagin = new Page<>(current, size, true);
        IPage<ReportTest> selectResult = reportTestMapper.selectByPage(pagin, queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param reportTest 实例对象
     * @return 实例对象
     */
    @Override
    public ReportTest insert(ReportTest reportTest) {
        reportTestMapper.insert(reportTest);
        return reportTest;
    }

    /**
     * 更新数据
     *
     * @param reportTest 实例对象
     * @return 实例对象
     */
    @Override
    public ReportTest update(ReportTest reportTest) {
        //1. 根据条件动态更新
//        LambdaUpdateChainWrapper<ReportTest> chainWrapper = new LambdaUpdateChainWrapper<ReportTest>(reportTestMapper);
//        if (StrUtil.isNotBlank(reportTest.getSignReport())) {
//            chainWrapper.eq(ReportTest::getSignReport, reportTest.getSignReport());
//        }
//        if (StrUtil.isNotBlank(reportTest.getTestInfo())) {
//            chainWrapper.eq(ReportTest::getTestInfo, reportTest.getTestInfo());
//        }
//        if (StrUtil.isNotBlank(reportTest.getMerge())) {
//            chainWrapper.eq(ReportTest::getMerge, reportTest.getMerge());
//        }
//        if (StrUtil.isNotBlank(reportTest.getBigMerge())) {
//            chainWrapper.eq(ReportTest::getBigMerge, reportTest.getBigMerge());
//        }
//        //2. 设置主键，并更新
//        chainWrapper.set(ReportTest::getId, reportTest.getId());
//        boolean ret = chainWrapper.update();
//        //3. 更新成功了，查询最最对象返回
//        if (ret) {
//            return queryById(reportTest.getId());
//        } else {
//            return reportTest;
//        }
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        int total = reportTestMapper.deleteById(id);
        return total > 0;
    }

    @Override
    public List<ReportTest> selectReportTestList(ReportTest reportTest) {
        List<ReportTest> reportTests = reportTestMapper.selectReportTestList();
        return reportTests;
    }
}
